﻿
namespace Patterns4Net.ArchitectureExplorer.ViewModels.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using Microsoft.Win32;

    [Export(typeof(IOpenFileDialog))]
    public class Win32OpenFileDialog : IOpenFileDialog
    {
        private OpenFileDialog dialog;

        private IList<string> extensions = new List<string>();

        public Win32OpenFileDialog()
        {
            this.dialog = new OpenFileDialog();            
        }

        public void AddExtension(string name, string extension)
        {
            this.extensions.Add(string.Format("{0}|{1}", name, extension));
        }

        public void Reset()
        {
            this.dialog.Filter = null;
            this.Multiselect = false;
        }

        public bool Multiselect
        {
            get { return this.dialog.Multiselect; }
            set { this.dialog.Multiselect = value; }
        }

        public string[] ShowDialog()
        {
            this.dialog.Filter = string.Join("|", this.extensions);
            bool? dialogResult = this.dialog.ShowDialog();
            if (dialogResult == false)
            {
                return null;
            }

            return this.dialog.FileNames;
        }

        public object Clone()
        {
            var result = new Win32OpenFileDialog();
            result.extensions = this.extensions;
            result.Multiselect = this.Multiselect;
            return result;
        }
    }
}
